package dp.gr03.payroll.classificationTransaction;

import java.util.GregorianCalendar;

import dp.gr03.payroll.classifications.HourlyClassification;
import dp.gr03.payroll.classifications.TimeCard;
import dp.gr03.payroll.exception.PayRollException;
import dp.gr03.payroll.payrollApplication.Transaction;
import dp.gr03.payroll.payrollDatabase.PayrollDatabase;
import dp.gr03.payroll.payrollDomain.Employee;
import dp.gr03.payroll.payrollDomain.PaymentClassification;

public class TimecardTransaction implements Transaction
{

    private GregorianCalendar date;
    private double hours;
    private int empId;

    public TimecardTransaction(GregorianCalendar date, double hours, int empId)
    {
        this.date = date;
        this.hours = hours;
        this.empId = empId;
    }

    @Override
    public void execute() throws PayRollException
    {
        Employee e = PayrollDatabase.getInstance().getEmployee(empId);
        if (e != null)
        {
            PaymentClassification pc = e.getPaymentClassification();
            if (pc instanceof HourlyClassification)
            {
                HourlyClassification hc = (HourlyClassification) pc;
                hc.addTimeCard(new TimeCard(date, hours));
            } else
            {
                throw new PayRollException("Tried to add timecard to non-hourly employee");
            }
        } else
        {
            throw new PayRollException("No such employee");
        }
    }
}
